package zev;

import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * @ProjectName: Learning
 * @Package: zev
 * @ClassName: Method
 * @Description: 非泛型类中定义泛型方法
 * @Author: ZhangZewei
 * @CreateDate: 2018/11/7 22:57
 * @UpdateUser: ZhangZewei
 * @UpdateDate: 2018/11/7 22:57
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
public class Method {

    // 泛型方法，在返回类型前面使用泛型字母
    public static <T> void test1(T t){
        System.out.println(t);
    }

    // T 只能是list 或者list 的子类
    public static <T extends List> void test2(T t){
        t.add("aa");
    }

    // T... 可变参数   --->   T[]
    public static <T extends Closeable> void test3(T...a) {
        for (T temp : a) {
            try {
                if (null != temp) {
                    temp.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        test1("java 是门好语言");
        test3(new FileInputStream("a.txt"));
    }
}
